package com.u3k.app.mid.utils.net;

import com.u3k.app.mid.MiddleMain;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.ae;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Request {
    Map<String, String> bodyKeyValues;
    String bodyText;
    Map<String, File> files;
    List<String> headers;
    String method = "GET";
    Object tag = this;
    String url;

    public Request addFile(File file) {
        if (this.files == null) {
            this.files = new HashMap();
        }
        this.files.put("filename", file);
        return this;
    }

    public Request addFile(String str, File file) {
        if (this.files == null) {
            this.files = new HashMap();
        }
        this.files.put(str, file);
        return this;
    }

    public Request addHeader(String str, String str2) {
        if (this.headers == null) {
            this.headers = new ArrayList();
        }
        this.headers.add(str);
        this.headers.add(str2);
        return this;
    }

    public Request addJson(String str) {
        this.bodyText = str;
        return this;
    }

    public Request addKeyValue(String str, String str2) {
        if (this.bodyKeyValues == null) {
            this.bodyKeyValues = new HashMap();
        }
        this.bodyKeyValues.put(str, str2);
        return this;
    }

    public Request addString(String str) {
        this.bodyText = str;
        return this;
    }

    public String bodyKeyValues() {
        if (this.bodyKeyValues == null || this.bodyKeyValues.size() == 0) {
            return null;
        }
        String str = "";
        for (Entry entry : this.bodyKeyValues.entrySet()) {
            if (str.length() > 0) {
                str = str + "&";
            }
            str = str + ((String) entry.getKey()) + "=" + ((String) entry.getValue());
        }
        return str;
    }

    public byte[] bodyKeyValues2Bytes() {
        if (this.bodyKeyValues == null || this.bodyKeyValues.size() == 0) {
            return null;
        }
        String str = "";
        for (Entry entry : this.bodyKeyValues.entrySet()) {
            str = (str.length() > 0 ? str + "&" : str) + ((String) entry.getKey()) + "=" + ((String) entry.getValue());
        }
        return str.getBytes("UTF-8");
    }

    public Request defaultHeader() {
        addHeader("Accept-Charset", "UTF-8");
        addHeader("Original-Encoding", "1");
        addHeader("Original-Encrypt", "ab");
        addHeader("X-AF-AppId", MiddleMain.getInstance().getAppId());
        addHeader("X-AF-Channel", MiddleMain.getInstance().getChannelId());
        addHeader("X-AF-ShellVer", MiddleMain.getInstance().getShellVersion());
        return this;
    }

    public String getHeader(String str) {
        if (this.headers == null) {
            return null;
        }
        for (int i = 0; i < this.headers.size(); i += 2) {
            if (str.equals(this.headers.get(i))) {
                return (String) this.headers.get(i + 1);
            }
        }
        return null;
    }

    public String host() {
        try {
            return new URL(this.url).getHost();
        } catch (Exception e) {
            e.printStackTrace();
            StatisticsManager.getInstance().logException(ae.a(), e);
            return null;
        }
    }

    public Request post() {
        this.method = "POST";
        return this;
    }

    public Request setHeader(String str, String str2) {
        if (this.headers == null) {
            this.headers = new ArrayList();
        }
        for (int i = 0; i < this.headers.size(); i += 2) {
            if (str.equals(this.headers.get(i))) {
                this.headers.remove(i + 1);
                this.headers.add(i + 1, str2);
                break;
            }
        }
        this.headers.add(str);
        this.headers.add(str2);
        return this;
    }

    public Request url(String str) {
        if (str == null) {
            throw new NullPointerException("url == null");
        }
        this.url = str;
        return this;
    }
}
